/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.dict.controller.assembler;

import com.google.common.collect.Lists;
import net.siufung.core.utils.CopyUtil;
import net.siufung.market.base.dict.controller.model.dict.DictVo;
import net.siufung.market.base.dict.mapper.entity.Dict;
import net.siufung.market.base.dict.mapper.entity.DictType;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 *
 * 字典数据 持久层展示层组装器
 *
 * @author 陈建峰
 * @since 2022-07-08 15:30:15
 */
public class DictAssembler {

    /**
     * Dict 转换成 DictVo
     * @param dict 数据库实体
     * @return DictVo 视图
     */
    public static DictVo from(Dict dict){
        if(!Objects.isNull(dict)){
            DictVo dictVo = new DictVo();
            CopyUtil.copyProperties(dict, dictVo);
            return dictVo;
        }
        return null;
    }

    /**
     * DictList 转换成 DictVoList
     * @param dictList 数据库实体集合
     * @return List<DictVo> 视图集合
     */
    public static List<DictVo> fromList(List<Dict> dictList){
        List<DictVo> dictVoList = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(dictList)){
            DictVo dictVo;
            for (Dict dict: dictList) {
                dictVo = new DictVo();
                CopyUtil.copyProperties(dict, dictVo);
                dictVoList.add(dictVo);
            }
        }
        return dictVoList;
    }

    /**
     * DictList 转换成 DictVoList
     * @param dictList 数据库实体集合
     * @return List<DictVo> 视图集合
     */
    public static List<DictVo> fromList(List<Dict> dictList, List<DictType> dictTypeList){
        List<DictVo> dictVoList = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(dictList)){
            DictVo dictVo;
            for (Dict dict: dictList) {
                dictVo = new DictVo();
                CopyUtil.copyProperties(dict, dictVo);
                for (DictType dictType : dictTypeList) {
                    if(dict.getDictTypeId().equals(dictType.getId())){
                        dictVo.setTypeCode(dictType.getTypeCode());
                        dictVo.setTypeName(dictType.getTypeName());
                        break;
                    }
                }
                dictVoList.add(dictVo);
            }
        }
        return dictVoList;
    }
}

